package com.mdd.admin.controller.region;

import com.github.xiaoymin.map.model.MapLocation;
import com.github.xiaoymin.map.request.tencent.AmapGenCoderRequest;
import com.github.xiaoymin.map.response.amap.tencent.AmapGenCoderResponse;
import com.mdd.admin.service.region.IDevRegionService;
import com.mdd.admin.vo.region.DevRegionListVo;
import com.mdd.common.config.map.LikeTenCentClient;
import com.mdd.common.enums.RegionLevelEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 地区管理
 */
@Api(tags = "地区管理")
@RestController
@RequestMapping("api/region")
public class DevRegionController {

    @Resource
    IDevRegionService iDevRegionService;

    @Resource
    private LikeTenCentClient mapClient;

    /**
     * 省市区树形结构
     *
     * @return Object
     */
    @ApiOperation(value = "省市区树形结构")
    @GetMapping("/tree")
    public List<DevRegionListVo> tree() {
        // 树形结构
        return iDevRegionService.tree();
    }

    /**
     * 省
     *
     * @return Object
     */
    @ApiOperation(value = "获取省")
    @GetMapping("/province")
    public List<DevRegionListVo> province() {
        // 国家下查省级数据
        return iDevRegionService.province(RegionLevelEnum.PROVINCE.getCode());
    }

    /**
     * 市
     *
     * @return Object
     */
    @ApiOperation(value = "获取城市")
    @GetMapping("/city")
    public List<DevRegionListVo> city(@RequestParam(value = "provinceId", required = false) Long provinceId) {
        // 省下查市数据
        return iDevRegionService.city(provinceId, RegionLevelEnum.CITY.getCode());
    }

    /**
     * 区
     *
     * @return Object
     */
    @ApiOperation(value = "获取县/区")
    @GetMapping("/district")
    public List<DevRegionListVo> district(@RequestParam(value = "cityId", required = false) Long cityId) {
        // 市下查县区数据
        return iDevRegionService.district(cityId, RegionLevelEnum.DISTRICT.getCode());
    }

    /**
     * 删除缓存
     *
     * @return Object
     */
    @ApiOperation(value = "删除缓存")
    @GetMapping("/del/cache")
    public void delCache() {
        // 删除缓存
        iDevRegionService.delCache();
    }

    /**
     * 根据经纬度获取详细地址
     *
     * @return Object
     */
    @PostMapping("/genCoder")
    public AmapGenCoderResponse genCoder(@RequestBody AmapGenCoderRequest request) {
        Optional<AmapGenCoderResponse> tencentGenCoderResponse = mapClient.genCoder(request);
        return tencentGenCoderResponse.get();
    }

    /**
     * 根据经纬度获取详细地址
     *
     * @return Object
     */
    @GetMapping("/keyWordAddress")
    public MapLocation genCoder(@RequestParam("keyWord") String keyWord) {
        return mapClient.address(keyWord);
    }

}
